var hintTextarea = "Enter Comment";
var hintTextbox = "Enter name";
var hintActiveClass= "inputTextActive";
var hintClass="inputText";

function loadAuthorPageImages(){
	opacity('bookImg', 1.0, 0.0, 0.8, 'aboutauthor');
	fadeIn('authorImg', 1.0, 'aboutauthor');
}

function validateCommentForm(){
	var author=document.forms["commentForm"]["author"].value;
	if (author==null || author=="" || author.trim()==hintTextbox)
	  {
		  alert("First name must be filled out");
		  return false;
	  }
	var comment=document.forms["commentForm"]["content"].value;
	if (comment==null || comment==""|| comment.trim()==hintTextarea)
	  {
		  alert("Please add comment");
		  return false;
	  }
}

function textCounter(obj) {
	var maxLimit = obj.maxLength;
	var comment=document.forms["commentForm"]["content"].value;
	
	var remainingCharacters = maxLimit - comment.length;
	var label = document.getElementById('remCharacters')
	label.innerHTML = "Remaining characters: " + remainingCharacters;
}


function onHintTextboxFocus(obj) {

  if (obj.value.trim()==hintTextbox||obj.value.trim()==hintTextarea) {
    obj.value = "";
    obj.className = hintActiveClass;
  }
}

function onHintTextboxBlur(obj) {

  if (obj.value.trim().length==0) {
	  if(obj.name=='content'){
		  obj.value = hintTextarea;
	  }else{
		  obj.value = hintTextbox;
	  }
	 obj.className = hintClass;
  }
}

if( !window.XMLHttpRequest ) 
	XMLHttpRequest = function(){
	try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
	try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
	throw new Error("Could not find an XMLHttpRequest alternative.")
};

//
//Makes an AJAX request to a local server function w/ optional arguments
//
//functionName: the name of the server's AJAX function to call
//opt_argv: an Array of arguments for the AJAX function
//
function Request(function_name, opt_argv) {

    if (!opt_argv)
      opt_argv = new Array();

    // Find if the last arg is a callback function; save it
    var callback = null;
    var len = opt_argv.length;
    if (len > 0 && typeof opt_argv[len-1] == 'function') {
      callback = opt_argv[len-1];
      opt_argv.length--;
    }
    var async = (callback != null);

    // Encode the arguments in to a URI
    var query = 'action=' + encodeURIComponent(function_name);
    for (var i = 0; i < opt_argv.length; i++) {
      var key = 'arg' + i;
      var val = JSON.stringify(opt_argv[i]);
      query += '&' + key + '=' + encodeURIComponent(val);
    }
    query += '&time=' + new Date().getTime(); // IE cache workaround

    // Create an XMLHttpRequest 'GET' request w/ an optional callback handler
    var req = new XMLHttpRequest();
    req.open('GET', '/comments?' + query, async);

    if (async) {
      req.onreadystatechange = function() {
        if(req.readyState == 4 && req.status == 200) {
          var response = null;
          try {
           response = JSON.parse(req.responseText);
          } catch (e) {	
           response = req.responseText;
          }
          callback(response);
        }
      }
    }

    // Make the actual request
    req.send(null);
  }

//Adds a stub function that will pass the arguments to the AJAX call
function InstallFunction(obj, functionName) {
	obj[functionName] = function() { Request(functionName, arguments); }
}

//Server object that will contain the callable methods
var server = {};

//Insert 'Add' as the name of a callable method
InstallFunction(server, 'getComments');

//Client function that calls a server rpc and provides a callback
function getComments() {
	server.getComments(onAddSuccess);
}

//Callback for after a successful doAdd
function onAddSuccess(response) {
	rotateComments(response);
}

var counter = 0;
function rotateComments(comments){
	var commentDiv = document.getElementById('comment')
	if(counter == comments.length){
		counter=0;
		var timer = setTimeout(function(){rotateComments(comments);},0);
	}else{
		commentDiv.innerHTML = "\"" + comments[counter].content + "\" - " + comments[counter].author;
		var fade = setTimeout(function(){fadeIn('comment',1.0,'commentScope');},0);
		var hide = setTimeout(function(){fadeOut('comment',1.0,'commentScope');},7000);
		counter = counter + 1;
		var timer = setTimeout(function(){rotateComments(comments);},10000);
	}
}

function fadeIn(id, durationValue,scopeValue){
	new Effect.Appear(id,
			{duration: durationValue},
			{queue:{position:'end', scope:scopeValue}});
	
}

function fadeOut(id, durationValue,scopeValue){
	new Effect.Fade(id,
			{duration: durationValue},
			{queue:{position:'end', scope:scopeValue}});
}

function opacity(id,fromValue,toValue,durationValue,scopeValue){
	new Effect.Opacity(id, 
			{ from: fromValue, to: toValue, duration: durationValue },
			{queue:{position:'end', scope:scopeValue}});
}
